package chatClient;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;

public class ChatterListUtil extends UnicastRemoteObject implements Remote{
	
	ChatClient client;
	
	protected ChatterListUtil(ChatClient client) throws RemoteException {
		this.client = client;
	}

	public void addChatter(String chatterName){
		VBox vb = client.chatterBox;
		for(Node n : vb.getChildren()){
			if(((Button)n).getText().equals(chatterName)){
				return;
			}
		}
		Button newChatterBtn = new Button(chatterName);
		newChatterBtn.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
				client.targetChatterName = newChatterBtn.getText();
				client.chatBox.getChildren().remove(0);
				client.chatBox.getChildren().add(0, (TextArea)client.taDisplayHash.get(newChatterBtn.getText()));
				client.setButtonCallingOff(newChatterBtn.getText());
			}
		});
		vb.getChildren().add(newChatterBtn);
	}
	
	public void removeChatter(String chatterName){
		VBox vb = client.chatterBox;
		for(Node n : vb.getChildren()){
			if(((Button)n).getText().equals(chatterName)){
				vb.getChildren().remove(n);
				client.chatBox.getChildren().remove(0);
				client.chatBox.getChildren().add(0, client.taDisplay);
				break;
			}
		}
	}
	
}
